#include "lvgl_font.h"
#include<QFile>
#include<QDebug>
lvgl_font::lvgl_font()
{
    __g_xbf_hd = {
        .min = 0x0020,
        .max = 0x9fa0,
        .bpp = 1,
    };
}

uint8_t *lvgl_font::__user_font_getdata(int offset, int size){
    //如字模保存在SPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size);
    //如字模已加载到SDRAM,直接返回偏移地址即可如:return (uint8_t*)(sdram_fontddr+offset);
    QFile file("F:\\Gitee\\STM32F405RGT6\\aurora-foc\\2.Software\\aurora_qt\\San16x.bin");
    file.open(QIODevice::ReadOnly);
    file.seek(offset);
    file.read((char *)buff,size);
    file.close();
    return buff;
}

uint8_t *lvgl_font::getFont16Data(uint32_t unicode_letter){
    if( unicode_letter>__g_xbf_hd.max || unicode_letter<__g_xbf_hd.min ) {
            return NULL;
        }
    uint32_t unicode_offset = sizeof(x_header_t)+(unicode_letter-__g_xbf_hd.min)*4;
    uint32_t *p_pos = (uint32_t *)__user_font_getdata(unicode_offset, 4);
    if( p_pos[0] != 0 ) {
        uint32_t pos = p_pos[0];
        glyph_dsc_t * gdsc = (glyph_dsc_t*)__user_font_getdata(pos, sizeof(glyph_dsc_t));
        w = gdsc->box_w;
        h = gdsc->box_h;
        return __user_font_getdata(pos+sizeof(glyph_dsc_t), gdsc->box_w*gdsc->box_h*__g_xbf_hd.bpp/8);
    }
    return NULL;
}
